package com.example.hello;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
public class listManager {

    private DBHelper dbHelper;
    private SPDBHelper SP_dbhelper;
    private String TBNAME;
    private String tb_sport;
    private static final String TAG = "listmanager";
    public listManager(Context context) {
        dbHelper = new DBHelper(context);
        SP_dbhelper = new SPDBHelper(context);
        TBNAME = DBHelper.TB_NAME;
        tb_sport = SPDBHelper.sp_TB_NAME;
    }
    @SuppressLint("Range")
    public float sum( ){
        float total=0.0f;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
      String sql = "select sum(curRate) from tb_rates";
        Cursor cursor = db.rawQuery(sql,null);

       if (cursor.moveToNext()){
          Float all = cursor.getFloat(cursor.getColumnIndex("sum(curRate)"));
          Log.i(TAG, String.valueOf(all));
            total = all;
           //cursor.close();
       }
      // db.close();
       return total;
    }
    @SuppressLint("Range")
    public float sp_sum( ){
        float total=0.0f;
        SQLiteDatabase db = SP_dbhelper.getReadableDatabase();
        String sql = "select sum(curSport) from tb_sport";
        Cursor cursor = db.rawQuery(sql,null);
        if (cursor.moveToNext()){
            Float sp_all = cursor.getFloat(cursor.getColumnIndex("sum(curSport)"));
            Log.i(TAG, String.valueOf(sp_all));
            total = sp_all;
        }
        return total;
    }
    public void add(RateItem item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("curname", item.getCurName());
        values.put("currate", item.getCurRate());
        db.insert(TBNAME, null, values);
        db.close();
    }
    public void sp_add(SportItem item){
        SQLiteDatabase db = SP_dbhelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("curname", item.getCurName());
        values.put("cursport", item.getCurSport());
        db.insert(tb_sport, null, values);
        db.close();
    }
    public void addAll(List<RateItem> list){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        for (RateItem item : list) {
            ContentValues values = new ContentValues();
            values.put("curname", item.getCurName());
            values.put("currate", item.getCurRate());
            db.insert(TBNAME, null, values);
        }
        db.close();
    }

    public void deleteAll(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME,null,null);
        db.close();
    }



    public void update(RateItem item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("curname", item.getCurName());
        values.put("currate", item.getCurRate());
        db.update(TBNAME, values, "ID=?", new String[]{String.valueOf(item.getId())});
        db.close();
    }

    @SuppressLint("Range")
    public List<RateItem> listAll() {
        List<RateItem> rateList = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, null, null, null, null, null);
        if(cursor!=null){
            rateList = new ArrayList<RateItem>();
            while(cursor.moveToNext()){
                RateItem item = new RateItem();
                item.setId(cursor.getInt(cursor.getColumnIndex("ID")));
                item.setCurName(cursor.getString(cursor.getColumnIndex("CURNAME")));
                item.setCurRate(cursor.getString(cursor.getColumnIndex("CURRATE")));
                rateList.add(item);
            }
            cursor.close();
        }
        db.close();
        return rateList;
    }
    @SuppressLint("Range")
    public List<SportItem> sp_listAll() {
        List<SportItem> sportList = null;
        SQLiteDatabase db = SP_dbhelper.getReadableDatabase();
        Cursor cursor = db.query(tb_sport, null, null, null, null, null, null);
        if(cursor!=null){
            sportList = new ArrayList<SportItem>();
            while(cursor.moveToNext()){
               SportItem item = new SportItem();
                item.setId(cursor.getInt(cursor.getColumnIndex("ID")));
                item.setCurName(cursor.getString(cursor.getColumnIndex("CURNAME")));
                item.setCurSport(cursor.getString(cursor.getColumnIndex("CURSPORT")));
                sportList.add(item);
            }
            cursor.close();
        }
        db.close();
        return sportList;
    }
    @SuppressLint("Range")
    public RateItem findById(int id){
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        Cursor cursor = db.query(TBNAME, null, "ID=?", new String[]{String.valueOf(id)}, null, null, null);
        RateItem rateItem = null;
        if(cursor!=null && cursor.moveToFirst()){
            rateItem = new RateItem();
            rateItem.setId(cursor.getInt(cursor.getColumnIndex("ID")));
            rateItem.setCurName(cursor.getString(cursor.getColumnIndex("CURNAME")));
            rateItem.setCurRate(cursor.getString(cursor.getColumnIndex("CURRATE")));
            cursor.close();
        }
        db.close();
        return rateItem;
    }
}
